{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e93e6131",
   "metadata": {},
   "outputs": [],
   "source": [
    "import mysql.connector\n",
    "mydb = mysql.connector.connect(user='gan', password='password',\n",
    "                              host='127.0.0.1',port=”3306”,) \n",
    "# 使用 cursor() 方法创建一个游标对象 cursor\n",
    "cursor = mydb.cursor()\n",
    "# 使用 execute() 方法执行 SQL，如果表存在则删除\n",
    "sql_2=\"SHOW TABLES\"\n",
    "cursor.execute(sql_2)\n",
    "result2=cursor.fetchall()\n",
    "print('result2:',result2) \n",
    "# 关闭数据库连接\n",
    "mydb.close()\n",
    "from sqlalchemy import create_engine\n",
    "import pandas as pd\n",
    "# 初始化数据库连接:\n",
    "engine=create_engine('mysql+mysqlconnector://root:password@localhost:3306/test')\n",
    "# 查询语句，选出employee表中的所有数据\n",
    "sql = \"SELECT * FROM fct_sales limit 8\" \n",
    "# read_sql_query的两个参数: sql语句， 数据库连接\n",
    "df_fct_sales = pd.read_sql_query(sql, engine)\n",
    "# 输出employee表的查询结果\n",
    "(df_fct_sales.head(3))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "692632a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "create_engine(\"数据库类型+数据库驱动://数据库用户名:数据库密码@IP地址:端口/数据库\"，其他参数)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
